package com.hnevc.exam4zhangsan;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShowActivity extends AppCompatActivity {
    List<Food> foodList;
    //定义控件变量mListView
    ListView mListView;

    TextView tvFoodName;
    TextView tvFoodPrice;
    TextView tvFoodDesc;
    ImageView ivFoodPic;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        //获取控件
        tvFoodName = (TextView) findViewById(R.id.id_tv_foodname);
        tvFoodDesc = (TextView) findViewById(R.id.id_tv_fooddesc);
        ivFoodPic = (ImageView) findViewById(R.id.id_iv_foodpic);

        mListView = (ListView) findViewById(R.id.id_lv_foods);
        //TODO 功能实现题：获取数据并更新界面
        new Thread(new Runnable() {
            @Override
            public void run() {
                //定义client对象
                OkHttpClient client = new OkHttpClient();
                //定义url,表示要访问的地址
                String url = "http://10.1.66.232:8080/RestaurantServer/Food/getFoods";
                //定义请求对象
                Request request = new Request.Builder()
                        .url(url)//表示请求的地址
                        .get()    //表示请求的访问为get
                        .build();  //构建request

                try {
                    Response response = client.newCall(request).execute();
                    String data = response.body().string();
                    //构建Gson解析对象
                    Gson gson = new Gson();
                    foodList = gson.fromJson(data,new TypeToken<List<Food>>(){}.getType());
                    //启用UI线程，更新界面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //创建适配器
                            MyBaseAdapter adapter = new MyBaseAdapter();
                            //使用适配器填充ListView
                            mListView.setAdapter(adapter);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
    //定义适配器对类型
    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return foodList.size();
        }

        @Override
        public Object getItem(int position) {
            return foodList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return foodList.get(position).getFoodId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //根据项目布局，获取视图对象
            convertView = LayoutInflater.from(ShowActivity.this)
                    .inflate(R.layout.list_item,null);
            //从刚才获取的视图对象中，获取显示食物名称的TextView
            TextView mTvFoodName = (TextView) convertView.findViewById(R.id.id_tv_foodname);
            //将mTvFoodName控件所显示的文字，设置为position位置的食物的名字
            mTvFoodName.setText(foodList.get(position).getFoodName());
            //TODO 功能实现2 显示食物的详细信息

            // TODO 显示图片,Picasso库
            //1、获取显示图片的控件
            ImageView ivPic = (ImageView) view.findViewById(R.id.id_iv_foodpic);
            //2、获取图片路径
            String imageurl =
                    "http://10.1.66.232:8080/RestaurantServer/uploadFoodImage/"
                            +Food.getFoodPic();
            //3、利用picasso加载显示图片到ivpic
            Picasso.with(ShowActivity.this)
                    .load(imageurl)
                    .into(ivPic);

            TextView tvFoodDesc = (TextView) view.findViewById(R.id.id_tv_fooddesc);
            tvFoodDesc.setText(food .getFoodDesc());

            //TODO 设置ListViewItem的点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 跳转到FoodDetailActivity
                    Intent intent = new Intent();
                    intent.setClass(ShowActivity.this,FoodDetailActivity.class);

                    //传递参数id
                    intent.putExtra("foodID",food.getFoodId());
                    startActivity(intent);



            //返回获取到的视图
            return convertView;
        }
    }
}
